//
//  MKRefreshFooterView.m
//  Category
//
//  Created by Burn on 2020/1/7.
//  Copyright © 2020 menksoft. All rights reserved.
//

#define Log NSLog(@"insetBottom = %f offsetY = >>>>%f, >>>>%f ",self.scrollView.insetBottom,self.scrollView.offsetY,(self.scrollView.height - self.scrollView.contentSize.height + MKRefreshContentInsetSpacing) - (MKRefreshContentInsetSpacing - self.scrollView.offsetY));


#import "MKRefreshFooterView.h"
#import "RefreshConst.h"
#import "UIView+Extension.h"
#import "UIScrollView+Extension.h"
@interface MKRefreshFooterView ()

@property(weak,nonatomic) id target;

@property(assign,nonatomic) SEL action;

@property(strong,nonatomic) UILabel *footerTitle;

@property(assign,nonatomic) BOOL panStateFooter;

@property(assign,nonatomic) CGFloat offsety;


@property(assign,nonatomic) BOOL  bottomSeting;


@end


@implementation MKRefreshFooterView

+ (instancetype)footerWithRefreshingTarget:(id)target refreshingAction:(SEL)action{
    
    return [[[MKRefreshFooterView alloc]init]footerWithRefreshingTarget:target refreshingAction:action];
}

- (void)endRefreshing{

    [UIView animateWithDuration:.25 animations:^{
        self.scrollView.contentInset = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, self.scrollView.contentInset.left, self.scrollView.contentInset.right);
    }];
    self.state = RefreshIdle;
    self.panStateFooter = NO;
}

- (instancetype)footerWithRefreshingTarget:(id)target refreshingAction:(SEL)action{
    
    MKRefreshFooterView *footer = [[MKRefreshFooterView alloc]init];
    footer.footerTitle = [[UILabel alloc]init];
    footer.target = target;
    footer.action = action;
    footer.footerTitle.text = @"footer";
    footer.backgroundColor = [UIColor orangeColor];
    [footer addSubview:self.footerTitle];
    return footer;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.frame = CGRectMake(0,self.scrollView.contentSize.height > self.scrollView.height ? self.scrollView.contentSize.height : self.scrollView.height, self.width, self.height);
    self.footerTitle.frame = self.bounds;

}


- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{
    [super scrollViewContentOffsetDidChange:change];
    if (self.state == RefreshRefreshing) {
        
        
        if (self.scrollView.insetBottom > 0 || self.bottomSeting) {
            
            CGFloat offset = (self.scrollView.height - self.scrollView.contentSize.height + MKRefreshContentInsetSpacing) - (MKRefreshContentInsetSpacing - self.scrollView.offsetY);
            
            // !!!: contentSizeHeight > scrollViewHeight时
            if (self.scrollView.height < self.scrollView.contentSize.height) {
                
                if (offset > MKRefreshContentInsetSpacing) {
                    self.scrollView.insetBottom = MKRefreshContentInsetSpacing;
                }else {
                    self.scrollView.insetBottom = offset <= 0 ? 0 : offset;
                    self.bottomSeting = YES;
                }
            }
            // !!!: contentSizeHeight < scrollViewHeight时
            else{
                
                if (self.scrollView.offsetY <= 0) {
                    self.scrollView.insetBottom = 0;
                    return;
                }
                
                CGFloat ib = offset >= 0 ? (offset > MKRefreshContentInsetSpacing + ( self.scrollView.height - self.scrollView.contentSize.height) ? MKRefreshContentInsetSpacing + ( self.scrollView.height - self.scrollView.contentSize.height) : offset) : 0;
                NSLog(@"ib = %f",ib);
                self.scrollView.insetBottom = ib;
                self.bottomSeting = YES;
            }
        }
        Log
        return;
    }

    if (self.panStateFooter && self.state == RefreshIdle) {
        
        if (self.scrollView.height < self.scrollView.contentSize.height) {
            //  滚动高度 - 自己的高度 - 已经滚动的高度 = 剩下的可以滚动高度 | 超出可滚动高度 会变成负数 self.offsety <= -MKRefreshContentInsetSpacing 也就是松开手就可以刷新
            if (self.scrollView.contentSize.height - self.scrollView.height - self.offsety <= -MKRefreshContentInsetSpacing) {
                self.state = RefreshRefreshing;
                [UIView animateWithDuration:.25 animations:^{
                    self.scrollView.insetBottom = MKRefreshContentInsetSpacing;
                }];
                [self.target performSelector:self.action];
            }
        }else{
            //  contentSizeHeight < self.Height 时 如果往上拉超出 MKRefreshContentInsetSpacing 也就是松开手就可以刷新
            if (self.offsety >= MKRefreshContentInsetSpacing) {
                self.state = RefreshRefreshing;
                [UIView animateWithDuration:.25 animations:^{
                    //  也就是把FooterView 发在最底下,如果直接 insetBottom = MKRefreshContentInsetSpacing 的话 FooterView 会紧挨着最后一个Cell,因为cell的最底部高度 < TableView最底部的高度
                    self.scrollView.insetBottom = self.scrollView.height - self.scrollView.contentSize.height + MKRefreshContentInsetSpacing;
                }];
                [self.target performSelector:self.action];
            }
        }
    }
    
}

- (void)scrollViewContentSizeDidChange:(NSDictionary *)change{
    [super scrollViewContentSizeDidChange:change];
    self.y = self.scrollView.contentSize.height > self.scrollView.height ? self.scrollView.contentSize.height : self.scrollView.height;
}

- (void)scrollViewPanStateDidChange:(NSDictionary *)change{

    [super scrollViewPanStateDidChange:change];

    self.panStateFooter = [change[@"new"] integerValue] == 3;
    self.offsety = self.scrollView.contentOffset.y;
    
    
}
 



@end
